<template>
	<div class="mainWrapper payApplication">
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/' }">
				<i class="lh lh-caiwu fw_100"></i>
			</el-breadcrumb-item>
			<el-breadcrumb-item>人才结算管理</el-breadcrumb-item>
			<el-breadcrumb-item>付款申请</el-breadcrumb-item>
		</el-breadcrumb>

		<div class="content_Box box-shadow">
			<div class="py_5 px_10">
				<el-row>
					<el-col :span="4">
						<el-button type="default" class="fl" @click="downloadOrder()" round size="mini">导出</el-button>
						<el-button type="default" class="fl" @click="showPaymentOrder()" round size="mini">生成付款单</el-button>
					</el-col>
					<el-col :span="20" class="fr">
						<el-form :inline="true" class="form-inline fr" @keyup.enter.native="shearchBtn()">
							<el-form-item class="w_140">
								<el-input v-model="sn" placeholder="付款申请编号"></el-input>
							</el-form-item>
							<el-form-item class="w_110">
								<el-select v-model="settlement_channel_sn" clearable placeholder="税源地">
									<el-option v-for="item in tlementList" :key="item.id" :label="item.name" :value="item.sn"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item class="w_100">
								<el-select v-model="status" clearable placeholder="状态">
									<el-option label="已生成" value="10"></el-option>
									<el-option label="待预审" value="15"></el-option>
									<el-option label="待审核" value="20"></el-option>
									<el-option label="已驳回" value="40"></el-option>
									<el-option label="审核成功" value="30"></el-option>
									<el-option label="已作废" value="50"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item class="w_100">
								<el-select v-model="type" clearable placeholder="付款单类型">
									<el-option label="平台" value="1"></el-option>
									<el-option label="商家" value="2"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item class="w_150">
								<el-autocomplete
									class="inline-input"
									v-model="business_name"
									:fetch-suggestions="querySearch3"
									placeholder="商户名称"
									:trigger-on-focus="true"
									@select="handleSelect3"
									clearable
								></el-autocomplete>
							</el-form-item>
							<el-form-item prop="start_time">
								<el-date-picker
									class="w_180i"
									v-model="start_time"
									type="datetime"
									value-format="yyyy-MM-dd HH:mm:ss"
									format="yyyy-MM-dd HH:mm:ss"
									default-time="00:00:00"
									placeholder="开始时间"
								>
								</el-date-picker> </el-form-item
							>~
							<el-form-item prop="end_time">
								<el-date-picker
									class="w_180i"
									v-model="end_time"
									type="datetime"
									value-format="yyyy-MM-dd HH:mm:ss"
									format="yyyy-MM-dd HH:mm:ss"
									default-time="23:59:59"
									placeholder="结束时间"
								>
								</el-date-picker>
							</el-form-item>
							<el-button size="mini" type="primary" class="round-right" native-type="button" @click="shearchBtn"
								>搜索</el-button
							>
							<el-button size="mini" round @click="resetBtn">重置</el-button>
						</el-form>
					</el-col>
				</el-row>
			</div>

			<div class="tableMaxBox">
				<template>
					<!-- 表格部分 -->
					<el-table :data="ListData" stripe>
						<el-table-column prop="id" type="index" label="序号" width="50px" align="center"></el-table-column>
						<el-table-column prop="sn" label="付款申请编号" width="150px"></el-table-column>
						<el-table-column prop="type" label="付款单类型" width="80px">
							<template slot-scope="{ row }">
								<span v-if="row.type == 1">平台</span>
								<span v-else-if="row.type == 2">商家</span>
							</template>
						</el-table-column>
						<el-table-column prop="business_name" label="商户名称" width="140px"></el-table-column>
						<el-table-column prop="shop_name" label="店铺名称" width="140px"></el-table-column>
						<el-table-column prop="bill_count" label="账单数量" width="80px">
							<template slot-scope="{ row }">
								<span class="u text-primary pointer" style="font-weight: bold;" @click="goToBillList(row)">{{
									row.bill_count
								}}</span>
							</template>
						</el-table-column>
						<!-- </el-table-column> -->
						<el-table-column prop="amount" label="金额" width="80px">
							<template slot-scope="{ row }">
								<span>{{ row.amount | filterNumber }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="rate" label="费率" width="80px">
							<template slot-scope="{ row }">
								<!-- <span v-if="row.rate != '/'">{{ row.rate }}%</span>
								<span v-if="row.rate == '/'">/</span> -->
								<span>{{ row.rate }}%</span>
							</template>
						</el-table-column>
						<el-table-column prop="rate_amount" label="服务费" width="80px">
							<template slot-scope="{ row }">
								<span>{{ row.rate_amount | filterNumber }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="total_amount" label="总费用" width="80px">
							<template slot-scope="{ row }">
								<span style="color: red;">{{ row.total_amount | filterNumber }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="settlement_name" label="结算通道名称" width="120px">
							<template slot-scope="{ row }">
								<span>{{ row.settlement_name }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="tax_source" label="税源地名称" width="180px"></el-table-column>
						<el-table-column prop="bank_card" label="账号" width="160px"></el-table-column>
						<el-table-column prop="rate" label="商户费率" width="80px">
							<template slot-scope="{ row }">
								<span v-if="row.type != 1">{{ row.business_rate }}%</span>
								<span v-if="row.type == 1">/</span>
							</template>
						</el-table-column>
						<el-table-column prop="business_rate_amount" label="商户服务费(元)" width="100px">
							<template slot-scope="{ row }">
								<span>{{ row.business_rate_amount }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="business_total_amount" label="商户总费用(元)" width="100px">
							<template slot-scope="{ row }">
								<span>{{ row.business_total_amount }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="status" label="状态">
							<template slot-scope="{ row }">
								<span class="text-primary" v-if="+row.status == 10">已生成</span>
								<span class="text-warning" v-else-if="+row.status == 15">待预审</span>
								<span class="text-warning" v-else-if="+row.status == 20">待审核</span>
								<span class="text-danger" v-else-if="+row.status == 40">已驳回</span>
								<span class="text-success" v-else-if="+row.status == 30">审核成功</span>
								<span class="text-danger" v-else-if="+row.status == 50">已作废</span>
							</template>
						</el-table-column>
						<el-table-column prop="creater_name" label="创建人" width="100px"></el-table-column>
						<el-table-column prop="add_time" label="创建时间" width="150px"></el-table-column>
						<el-table-column label="操作" fixed="right" width="180px" align="left">
							<template slot-scope="{ row }">
								<el-link
									v-if="(+row.status == 10 && row.type == 1) || (+row.status == 40 && row.type == 1)"
									:underline="false"
									type="primary"
									class="InfoFont"
									@click="showAuditOrder(row, 1)"
									>申请支付</el-link
								>
								<el-link
									v-if="+row.status != 50"
									:underline="false"
									style="color: #409eff;"
									class="InfoFont"
									@click="billDownloadOrder(row)"
									>导出明细</el-link
								>
								<el-link
									v-if="+row.status == 20 && +row.status != 50"
									:underline="false"
									type="warning"
									class="InfoFont"
									@click="showAuditOrder(row, 2)"
									>审核</el-link
								>
								<el-link
									v-if="+row.status == 15 && row.type == 1"
									:underline="false"
									type="warning"
									class="InfoFont"
									@click="showAuditOrder(row, 2)"
									>预审</el-link
								>
								<!-- v-if="+row.status == 10 || +row.status == 40 || +row.bill_count == 0" -->
								<el-link
									v-if="(+row.status == 10 && row.type == 1) || (+row.status == 40 && row.type == 1)"
									:underline="false"
									type="danger"
									class="InfoFont"
									@click="invalidOrder(row)"
									>作废</el-link
								>
								<el-link :underline="false" type="info" class="InfoFont" @click="cordTicketList(row)">操作记录</el-link>
								<el-link
									v-if="(+row.status == 10 && row.type == 1) || (+row.status == 40 && row.type == 1)"
									:underline="false"
									type="success"
									class="InfoFont"
									@click="showPaymentOrder(row)"
									>追加账单</el-link
								>
							</template>
						</el-table-column>
					</el-table>
					<!-- 分页组件 -->
					<page :total="total" :pageNums="pageNum" @pagedata="onChildValue"></page>
				</template>
			</div>
		</div>
		<!-- 审核状态-弹框 -->
		<el-dialog
			title="审核"
			:visible.sync="auditOrderStatus"
			width="560px"
			:close-on-click-modal="false"
			:before-close="hiddenRY"
		>
			<div class="auditCentent">
				<div class="row-info clear">
					<div class="fl title-key">付款公司名称：</div>
					<div class="fl list-cont">
						{{ audit.row.tax_source }}
					</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">金额：</div>
					<div class="fl list-cont">{{ audit.row.amount }}元（{{ audit.row.amount | convertCurrency }}）</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">服务费：</div>
					<div class="fl list-cont">{{ audit.row.rate_amount }}元（含个人所得税、增值税、平台服务费）</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">总费用：</div>
					<div class="fl list-cont">
						{{ audit.row.total_amount }}元（{{ audit.row.total_amount | convertCurrency }}）
					</div>
				</div>
				<div v-if="audit.type == 2">
					<div class="row-info clear">
						<div class="fl title-key">审核：</div>
						<div class="fl list-cont">
							<el-radio v-model="audit.status" label="30">审核成功</el-radio>
							<el-radio v-model="audit.status" label="40">审核失败</el-radio>
						</div>
					</div>
					<div class="row-info clear" v-if="audit.status == 40">
						<div class="fl title-key"><i class="lhadmin lhadmin-wujiaoxing"></i>失败原因</div>
						<div class="fl list-cont">
							<el-input type="textarea" :rows="4" :cols="32" v-model="audit.remark"></el-input>
						</div>
					</div>
					<div class="clear" v-if="audit.status == 30">
						<div class="fl list-cont" style="color: red; margin-left: 90px;">
							审核前请确认将总费用金额打款至付款公司账户
						</div>
					</div>
				</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<span class="btnOK mr_20" @click="toAuditOrder()">{{ audit.type == 2 ? '确 定' : '申 请' }}</span>
				<span class="resetBtn" @click="hiddenRY">取 消</span>
			</div>
		</el-dialog>
		<!-- 操作记录-弹框 -->
		<el-dialog
			:title="cordTicket.diaTitle + '：' + cordTicket.sn"
			:visible.sync="cordTicket.status"
			width="600px"
			:close-on-click-modal="false"
			:before-close="hiddenRY"
		>
			<div class="auditCentent">
				<div class="timeline">
					<el-button type="success" class="fl" round size="mini">创建付款单</el-button>
					<span>{{ cordTicket.row.add_time }}</span>
					<span>{{ cordTicket.row.creater_name }}</span>
				</div>
			</div>
			<div class="auditCentent" v-if="cordTicket.type > 10 && cordTicket.type != 50">
				<div class="timeline">
					<el-button type="success" class="fl" round size="mini">申请支付</el-button>
					<span>{{ cordTicket.row.apply_pay_time }}</span>
					<span>{{ cordTicket.row.apply_name }}</span>
				</div>
			</div>
			<div class="auditCentent" v-if="cordTicket.type == 50">
				<div class="timeline">
					<el-button type="danger" class="fl" round size="mini">作废</el-button>
					<span>{{ cordTicket.row.last_time }}</span>
					<span>{{ cordTicket.row.void_name }}</span>
				</div>
			</div>
			<div class="auditCentent" v-if="cordTicket.type >= 20 && cordTicket.type != 50">
				<div class="timeline">
					<el-button type="success" class="fl" round size="mini">
						{{ cordTicket.type != 40 || cordTicket.turnVerify == 1 ? '预审核成功' : '预审核失败' }}
					</el-button>
					<span>{{ cordTicket.row.pre_audit_time }}</span>
					<span>{{ cordTicket.row.pre_audit_name }}</span>
					<span v-if="cordTicket.type == 40 && cordTicket.turnVerify != 1" style="color: red;"
						>失败原因：{{ cordTicket.row.remark }}</span
					>
				</div>
			</div>
			<div
				class="auditCentent"
				v-if="
					(cordTicket.type == 30 && cordTicket.row.audit_time) || (cordTicket.type == 40 && cordTicket.row.audit_time)
				"
			>
				<div class="timeline">
					<el-button type="success" class="fl" round size="mini">
						{{ cordTicket.type == 30 ? '审核成功' : '审核失败' }}
					</el-button>
					<span>{{ cordTicket.row.audit_time }}</span>
					<span>{{ cordTicket.row.audit_name }}</span>
					<span v-if="cordTicket.type != 30" style="color: red;">失败原因：{{ cordTicket.row.remark }}</span>
				</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<span class="resetBtn" @click="hiddenRY()">关 闭</span>
			</div>
		</el-dialog>
		<!-- 生成付款单-弹框 -->
		<el-dialog
			title="生成付款单"
			:visible.sync="paymentOrderStatus"
			width="940px"
			:close-on-click-modal="false"
			:before-close="hiddenRY"
		>
			<div class="auditCentent tz">
				<div class="auditCentent_next">
					<div class="row-info clear">
						<div class="fl title-key">结算通道</div>
						<div class="fl list-cont bb">
							<el-select v-model="paymentOrder.settlement_channel_sn" @change="changeChannelList">
								<el-option
									v-for="(item, index) in channelList"
									:key="index"
									:label="item.name"
									:value="item.sn"
								></el-option>
							</el-select>
						</div>
						<div class="fl title-key">费率（%）</div>
						<div class="fl list-cont feilv">
							<el-input v-model="paymentOrder.rate" class="w_80i" disabled></el-input>
						</div>
					</div>
					<div class="row-info clear">
						<div class="fl title-key">账单编号</div>
						<div class="fl list-cont bb">
							<el-input
								type="textarea"
								:rows="3"
								:cols="28"
								size="mini"
								placeholder="请填写"
								v-model="paymentOrder.task_sn"
							></el-input>
						</div>
					</div>
					<div class="row-info clear">
						<div class="fl title-key">账单创建时间</div>
						<div class="fl list-cont bb">
							<el-date-picker
								class="w_180i"
								v-model="paymentOrder.start_time"
								type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss"
								format="yyyy-MM-dd HH:mm:ss"
								default-time="00:00:00"
								placeholder="开始时间"
								:clearable="false"
							>
							</el-date-picker>
							<span style="margin: 0 10px 0 20px;">~</span>
							<el-date-picker
								class="w_180i"
								v-model="paymentOrder.end_time"
								type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss"
								format="yyyy-MM-dd HH:mm:ss"
								default-time="23:59:59"
								placeholder="结束时间"
								:clearable="false"
							>
							</el-date-picker>
							<span class="btnOK ml_20" @click="getBillList()">查询</span>
							<span class="ml_20">账单条数：{{ billDataCount }}条</span>
						</div>
					</div>
				</div>
				<el-table :data="billData" style="width: 100%;" height="400px" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55"> </el-table-column>
					<el-table-column prop="id" type="index" label="序号" width="50"></el-table-column>
					<el-table-column prop="sn" label="账单编号" width="120">
						<template slot-scope="{ row }">
							<span>{{ row.sn }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="staff_name" label="账单对象" width="80">
						<template slot-scope="{ row }">
							<span>{{ row.staff_name }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="bill_date" label="账单日期" width="120">
						<template slot-scope="{ row }">
							<span>{{ row.bill_date }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="type" label="账单类型" width="110">
						<template slot-scope="{ row }">
							<span class="text-success" v-if="row.type == 1">任务账单</span>
							<span class="text-primary" v-if="row.type == 2">扣罚账单</span>
							<span class="text-primary" v-if="row.type == 3">平台激励账单</span>
						</template>
					</el-table-column>
					<el-table-column prop="amount" label="账单金额" width="100">
						<template slot-scope="{ row }">
							<span>{{ row.amount | filterNumber }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="count" label="数量">
						<template slot-scope="{ row }">
							<span class="u text-primary pointer" style="font-weight: bold;" @click="goToList(row)">{{
								row.count
							}}</span>
						</template>
					</el-table-column>
					<el-table-column prop="status" label="状态" width="120">
						<template slot-scope="{ row }">
							<span class="text-primary" v-if="row.status == 10">已生成</span>
							<span class="text-primary" v-if="row.status == 13">已确认</span>
							<span class="text-primary" v-if="row.status == 17">待审核</span>
							<span class="text-primary" v-if="row.status == 20">待提现</span>
							<span class="text-primary" v-if="row.status == 25">提现中</span>
							<span class="text-primary" v-if="row.status == 30">已提现</span>
							<span class="text-primary" v-if="row.status == 70">异常</span>
						</template>
					</el-table-column>
					<el-table-column prop="add_time" label="创建时间" width="150"></el-table-column>
				</el-table>
				<pages :total="totals" :pageNums="pageNums" @pagedata="onChildValues"></pages>
			</div>
			<div slot="footer" class="dialog-footer">
				<span class="btnOK mr_20" @click="createOrder()">生 成</span>
				<span class="resetBtn" @click="hiddenRY()">关 闭</span>
			</div>
		</el-dialog>
	</div>
</template>

<script>
var common = require('../../../static/tool.js')
import {
	paymentOrderList,
	fetchtlementChannel,
	paymentOrderAudit,
	paymentOrderPreAudit,
	paymentOrderInvalid,
	paymentOrderApply,
	addOrder,
	addToOrder
} from '@/api/talent.js'
import { fetchStaffList } from '@/api/bill.js'
import { fetchBusinessForSearchList } from '@/api/shop.js'
import dayjs from 'dayjs'
import axios from 'axios'
export default {
	name: 'TalentContract',
	data() {
		return {
			url: common.url,
			//搜索数据
			sn: '',
			settlement_channel_sn: '',
			status: '',
			start_time: '',
			end_time: '',
			type: '',
			business_name: '',
			business_sn: '',

			tlementList: [],
			ListData: [],
			billData: [],
			billDataCount: 0,

			total: 0,
			pageNum: 1,
			pageSizeNow: 20,

			totals: 0,
			pageNums: 1,
			pageSizeNows: 100,

			auditOrderStatus: false,
			paymentOrderStatus: false,
			audit: {
				sn: '',
				type: '',
				row: {},
				status: '',
				remark: ''
			},
			paymentOrder: {
				settlement_channel_sn: '',
				rate: '',
				task_sn: '',
				start_time: '',
				end_time: ''
			},
			cordTicket: {
				status: false,
				diaTitle: '',
				sn: '',
				type: '',
				diaTitle: '',
				row: ''
			},
			multipleSelection: [],
			// 商户列表
			merchantsLists: [],
			channelList: []
		}
	},
	created() {
		this.gettlementChannel()
		this.getMerchants()
	},
	mounted() {},
	methods: {
		//获取商户列表
		async getMerchants() {
			try {
				let ret = await fetchBusinessForSearchList()
				console.log(ret)
				ret.list.forEach(item => {
					item.value = item.name
				})
				this.merchantsLists = ret.list
			} catch (err) {
				console.log(err)
			}
		},
		//商户的自动完成
		querySearch3(queryString, cb) {
			var restaurants = this.merchantsLists
			var results = queryString ? restaurants.filter(this.createFilter3(queryString)) : restaurants
			// 调用 callback 返回建议列表的数据
			cb(results)
		},
		createFilter3(queryString) {
			return restaurant => {
				return restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
			}
		},
		handleSelect3(item) {
			this.business_name = item.name
			this.business_sn = item.business_sn
		},

		async fetchTableData() {
			try {
				let ret = await paymentOrderList({
					page: this.pageNum,
					pageSize: this.pageSizeNow,
					sn: this.sn,
					settlement_channel_sn: this.settlement_channel_sn,
					status: this.status,
					start_time: this.start_time,
					end_time: this.start_time,
					type: this.type,
					business_sn: this.business_sn
				})
				this.ListData = ret.list
				this.total = ret.count
			} catch (err) {}
		},
		// 获取结算通道
		async gettlementChannel() {
			try {
				let ret = await fetchtlementChannel()
				this.tlementList = ret.data.list
			} catch (err) {
				console.log(err)
			}
		},
		shearchBtn() {
			// 搜索
			this.pageNum = 1
			this.fetchTableData()
		},
		async resetBtn() {
			//重置
			await this.resetData() //重置请求数据
			this.pageNum = 1
			this.fetchTableData()
		},
		resetData() {
			//重置请求数据
			this.pageNum = 1
			this.sn = ''
			this.settlement_channel_sn = ''
			this.status = ''
			this.start_time = ''
			this.end_time = ''
			this.type = ''
			this.business_sn = ''
			this.business_name = ''
		},
		//商户调账记录-导出
		downloadOrder() {
			let obj = {
				token: JSON.parse(sessionStorage.getItem('manageInfo')).token,
				operation: 'export',
				sn: this.sn,
				settlement_channel_sn: this.settlement_channel_sn,
				status: this.status,
				start_time: this.start_time,
				end_time: this.end_time,
				type: this.type,
				business_sn: this.business_sn
			}
			let param = $.param(obj)
			window.location.href = this.url + '/paymentOrder/getList?' + param
		},
		//分页组件->子组件传值
		onChildValue(item) {
			this.pageNum = item[0]
			this.pageSizeNow = item[1]
			this.fetchTableData()
		},
		onChildValues(item) {
			this.pageNums = item[0]
			this.pageSizeNows = item[1]
			this.getBillList()
		},
		async showAuditOrder(row, index) {
			this.auditOrderStatus = true
			this.audit.type = index
			this.audit.sn = row.sn
			this.audit.status = '30'
			this.audit.row = row
		},
		async toAuditOrder(row) {
			try {
				if (this.audit.type == 2) {
					if (+this.audit.status == 40 && !this.audit.remark) {
						return this.$message.error('请填写失败原因！')
					}
					if (+this.audit.row.status === 20) {
						await paymentOrderAudit({
							sn: this.audit.sn,
							status: this.audit.status,
							remark: this.audit.remark
						})
					} else if (+this.audit.row.status === 15) {
						await paymentOrderPreAudit({
							sn: this.audit.sn,
							status: +this.audit.status === 30 ? '20' : this.audit.status,
							remark: this.audit.remark
						})
					}

					this.$message.success('审核成功！')
					this.hiddenRY()
					this.fetchTableData()
				} else {
					await paymentOrderApply({
						sn: this.audit.sn
					})

					this.$message.success('申请成功！')
					this.hiddenRY()
					this.fetchTableData()
				}
			} catch (err) {
				// console.log(err)
			}
		},
		cordTicketList(row) {
			for (let prop in row) {
				if (row[prop] === '0000-00-00 00:00:00' || row[prop] === null) {
					row[prop] = ''
				}
			}

			// 时间对比 终审时间大于预审和时间 1  ||  2
			const date1 = dayjs(row.audit_time)
			const date2 = dayjs(row.pre_audit_time)
			this.cordTicket.turnVerify = date1.diff(date2) > 0 ? 1 : 2
			this.cordTicket.status = true
			this.cordTicket.sn = row.sn
			this.cordTicket.row = row
			this.cordTicket.type = row.status
			this.cordTicket.diaTitle = '操作记录'
		},
		hiddenRY() {
			this.auditOrderStatus = false
			this.paymentOrderStatus = false
			this.cordTicket.status = false
			this.audit = {
				sn: '',
				row: {},
				status: '',
				remark: '',
				settlement_channel_sn: '',
				rate: ''
			}
			this.billData = []
			this.paymentOrder.start_time = this.$moment().add('days', 30).format('YYYY-MM-DD') + ' 00:00:00'
			this.paymentOrder.end_time = this.$moment().format('YYYY-MM-DD') + ' 23:59:59'
		},
		async getChannelList() {
			try {
				let ret = await fetchtlementChannel({
					status: 1,
					is_balance: 2
				})
				this.channelList = ret.list
				this.paymentOrder.settlement_channel_sn = ret.list.filter(x => x.is_default == 1)[0].sn || ''
				this.paymentOrder.rate = this.returnFloat(ret.list.filter(x => x.is_default == 1)[0].rate) || ''
			} catch (err) {
				console.log(err)
			}
		},
		changeChannelList(value) {
			console.log(value)
			this.paymentOrder.rate = this.returnFloat(this.channelList.filter(x => x.sn == value)[0].rate)
		},
		returnFloat(value) {
			var value = Math.round(parseFloat(value) * 100) / 100
			var xsd = value.toString().split('.')
			if (xsd.length == 1) {
				value = value.toString() + '.00'
				return value
			}
			if (xsd.length > 1) {
				if (xsd[1].length < 2) {
					value = value.toString() + '0'
				}
				return value
			}
		},
		async invalidOrder(row) {
			let ret = await this.$confirm('确认要将该付款单作废吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).catch(err => err)
			if (ret === 'cancel') return

			try {
				await paymentOrderInvalid({
					sn: row.sn
				})
				this.$message.success('操作成功！')
				this.hiddenRY()
				this.fetchTableData()
			} catch (err) {
				console.log(err)
			}
		},
		//生成付款单
		showPaymentOrder(row) {
			this.show_bill_sn = row ? row.sn : ''
			this.paymentOrder.start_time = this.$moment().subtract('days', 30).format('YYYY-MM-DD') + ' 00:00:00'
			this.paymentOrder.end_time = this.$moment().format('YYYY-MM-DD') + ' 23:59:59'
			this.paymentOrderStatus = true
			this.getChannelList()
			this.getBillList()
		},
		//跳转查看列表
		goToList(row) {
			if (row.status == 70) {
				//异常状态-跳转任务列表
				this.$router.push({
					path: '/tasklist',
					query: {
						bill_task_sn: row.detail + '',
						bill_status: 1
					}
				})
				return
			}
			if (row.type == 1 || row.type == 3) {
				// 跳转平台任务账单列表（应付账单）
				this.$router.push({
					path: '/PayDetail',
					query: {
						bill_staff_sn: row.sn
					}
				})
			} else {
				// 跳转扣罚账单列表
				this.$router.push({
					path: '/TicketLists',
					query: {
						bill_staff_sn: row.sn
					}
				})
			}
		},
		goToBillList(row) {
			this.$router.push({
				path: '/staffList',
				query: {
					from: 'payapplication',
					payment_order_sn: row.sn
				}
			})
		},
		async getBillList() {
			var sn = this.paymentOrder.task_sn
				.replace(/\n/g, ',')
				.split(',')
				.filter(function (s) {
					return s && s.trim()
				})
			let ret = await fetchStaffList({
				page: this.pageNums,
				pageSize: this.pageSizeNows,
				sn: sn + '',
				status: 13,
				time_type: 1,
				flow_path: 1,
				start_time: this.paymentOrder.start_time,
				end_time: this.paymentOrder.end_time
			})

			this.billData = ret.list
			this.billDataCount = ret.count
			this.totals = ret.count
		},
		handleSelectionChange(val) {
			//获取用户的选中
			this.multipleSelection = []
			val.forEach(item => {
				this.multipleSelection.push(item.sn)
			})
			this.multipleSelection = Array.from(new Set(this.multipleSelection))
		},
		async createOrder() {
			try {
				if (+this.multipleSelection.length == 0) {
					// this.hiddenRY()
					this.$message.error('请勾选人才账单！')
					return
				}

				let obj = {
					bill_sn: '' + this.multipleSelection,
					settlement_channel_sn: this.paymentOrder.settlement_channel_sn
				}
				if (this.show_bill_sn) {
					obj = {
						...obj,
						sn: this.show_bill_sn
					}
					let ret = await addToOrder(obj)
				} else {
					let ret = await addOrder(obj)
				}

				this.$message.success(this.show_bill_sn ? '追加付款单成功！' : '生成付款单成功！')
				this.hiddenRY()
				this.fetchTableData()
			} catch (err) {
				console.log(err)
			}
		},
		//人才账单-导出
		billDownloadOrder(row) {
			if (+row.bill_count == 0) {
				this.$message.error('当前账单数量为0！')
				return
			}
			let obj = {
				token: JSON.parse(sessionStorage.getItem('manageInfo')).token,
				sn: row.bill_detail
			}
			// let param = $.param(obj)
			// window.location.href = this.url + '/bill/exportBillList?' + param
			var that = this
			axios
				.get(this.url + '/bill/exportBillList', {
					params: obj
				})
				.then(function (response) {
					that.$alert('已添加下载任务，请到下载管理中查看', '', {
						confirmButtonText: '确定'
					})
				})
				.catch(function (error) {
					alert(error)
				})
		}
	}
	// beforeRouteLeave(to, from, next) {
	// 	// 设置下一个路由的 meta
	// 	to.meta.keepAlive = false
	// 	next()
	// }
}
</script>

<style scoped>
.row-info .title-key {
	width: 108px;
}

.timeline {
	height: 30px;
	line-height: 30px;
	margin-bottom: 20px;
}

.timeline span {
	margin-left: 10px;
}

.auditCentent .row-info {
	margin: 0 0 10px;
}
</style>
<style>
.payApplication .el-checkbox__input {
	display: block !important;
}
</style>



// WEBPACK FOOTER //
// src/components/talentSettlement/PaymentApplication.vue